<?php
/**
* @version 1.0.0
* @package RSSearch! 1.0.0
* @copyright (C) 2011 www.rsjoomla.com
* @license GPL, http://www.gnu.org/licenses/gpl-2.0.html
*/
defined( '_JEXEC' ) or die( 'Restricted access' );
 

jimport('joomla.plugin.plugin');
require_once(JPATH_SITE.DS.'modules'.DS.'mod_rssearch'.DS.'helper.php');
class plgRSSearchContent extends JPlugin
{
	function plgRSSearchContent( &$subject )
	{
	    parent::__construct( $subject );
		jimport('joomla.html.parameter');
	    $this->_plugin = JPluginHelper::getPlugin('rssearch', 'content');
	    $this->_params = new JParameter($this->_plugin->params);
	}
	function getResults(&$results, $search, $params)
	{
	    $app = &JFactory::getApplication();
	    $db =& JFactory::getDBO();
		
	    
	    $fields = (array)$this->_params->get('searchfields','');
	    $categories = (array) $this->_params->get('categories','');
	    $sections = (array) $this->_params->get('sections','');
	    $orderby = $this -> _params->get('orderby','title');
	    $asc_desc = $this -> _params->get('asc_desc','ASC');
	    $itemId = $params->get('itemid','');
	    $itemId = (empty($itemId) ? '' : '&Itemid='.$itemId);

		foreach($fields as $field){
			if($field == 'content'){
				$tablecolumns = array('title','hits','ordering','created');
				$where_catid = '';

				if (in_array('categories', $fields)){
					if(modRSSearchHelper::isJ16()){
						if(!in_array('', $categories)){
							JArrayHelper::toInteger($categories['categories']);
							$where_catid = " AND `#__content`.`catid` IN (".implode(',', $categories['categories']).")";
						}
					}else {
						if(!in_array('', $categories)){
							JArrayHelper::toInteger($categories);
							$where_catid = " AND `#__content`.`catid` IN (".implode(',', $categories).")";
						}
					}	
				}

				$query = "SELECT #__content.id, #__content.title, #__content.alias, #__content.catid, #__content.introtext, #__categories.alias as categoryname FROM #__content 
				LEFT JOIN #__categories ON #__content.catid = #__categories.id
				WHERE (#__content.title LIKE '%".$search."%' OR #__content.introtext LIKE '%".$search."%' OR #__content.fulltext LIKE '%".$search."%') AND #__content.state = '1' ".$where_catid."
				GROUP BY #__content.id ORDER BY ".(in_array($orderby,$tablecolumns) ? '#__content.'.$orderby : '')." ".$asc_desc."";

				$db -> setQuery($query);
				$list = $db->loadObjectList();
				foreach ($list as $item)
				{
					$tmp = new stdClass();
					$tmp->title = $item->title;
					if(modRSSearchHelper::isJ16())
						$tmp->link = JRoute::_('index.php?option=com_content&view=article&id='.$item->id.':'.$item->alias.'&catid='.$item->catid.$itemId);
					else
						$tmp->link = JRoute::_('index.php?option=com_content&view=article&id='.$item->id.':'.$item->alias.( $item->catid != 0 ? '&catid='.$item->catid.':'.JFilterOutput::stringURLSafe($item->categoryname) : '').$itemId);

					$tmp->text = $item ->introtext;
					$results[] = $tmp;
				}
			}elseif($field == 'categories'){
				$tablecolumns = array('title','ordering');
				
				//ordering column- no longer in categories J 1.7
				
				$where_id = '';

				if(modRSSearchHelper::isJ16()){
					if(!in_array('', $categories)){
						JArrayHelper::toInteger($categories['categories']);
						$where_id = " AND `#__categories`.`id` IN (".implode(',', $categories['categories']).") AND `extension` = 'com_content'";
					}
				}else {
					if(!in_array('', $categories)){
						JArrayHelper::toInteger($categories);
						$where_id = " AND `#__categories`.`id` IN (".implode(',', $categories).")";
					}
				}

				$query = "SELECT `id`,`title`, `alias`,`description` FROM #__categories 
				WHERE (`title` LIKE '%".$search."%' OR `description` LIKE '%".$search."%') AND `published` = '1' ".$where_id."
				GROUP BY `id`,`title`, `description` ORDER BY ".(in_array($orderby,$tablecolumns) ? $orderby : "title")." ".$asc_desc."";
				$db -> setQuery($query);
				$list = $db->loadObjectList();

				if(!empty($list)){
					foreach ($list as $item)
					{
						$tmp = new stdClass();
						$tmp->title = $item->title;
						if(modRSSearchHelper::isJ16())
							$tmp->link = JRoute::_('index.php?option=com_content&view=category&id='.$item->id.$itemId);
						else
							$tmp->link = JRoute::_('index.php?option=com_content&view=category&id='.$item->id.':'.$item->alias.$itemId);
							
				
						$tmp->text = $item->description;
						$results[] = $tmp;
					}
				}
			}elseif($field == 'sections'){
				$tablecolumns = array('title','ordering');
				if(modRSSearchHelper::isJ16()){
					JArrayHelper::toInteger($sections['sections']);
					$where_id = " AND `#__sections`.`id` IN (".implode(',', $sections['sections']).")";
				}else {
					JArrayHelper::toInteger($sections);
					$where_id = " AND `#__sections`.`id` IN (".implode(',', $sections).")";
				}

				$query = "SELECT `id`,`title`, `description`,`alias` FROM #__sections 
				WHERE (`title` LIKE '%".$search."%' OR `description` LIKE '%".$search."%') AND `published` = '1' ".$where_id."
				GROUP BY `id`,`title`, `description` ORDER BY ".(in_array($orderby,$tablecolumns) ? $orderby : "ordering")." ".$asc_desc."";

				$db -> setQuery($query);
				$list = $db->loadObjectList();
				foreach ($list as $item)
				{
					$tmp = new stdClass();
					$tmp->title = $item->title;
					$tmp->link = JRoute::_('index.php?option=com_content&view=section&id='.$item->id.$itemId);
					$tmp->text = $item->description;
					$results[] = $tmp;
				}
			}
		}
	    return true;
	}
}
?>